﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Data.SqlClient;

namespace LicPdaService
{
    public class PDAXmlInventoryAllotConfirmProcess
    {
        /// <summary>
        /// 9.2通过Serial出货分配库位确认
        /// DataSource:Orders
        /// </summary>

        public static string GetPDAInventoryAllotConfirmData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string serial = string.Empty;
            string orderkey = string.Empty;
            string sku = string.Empty;
            string skudescr = string.Empty;
            string batch = string.Empty;
            string qty = string.Empty;
            string lpn = string.Empty;
            string loc = string.Empty;
            string lot = string.Empty;
            string line_no = string.Empty;
            string uom = string.Empty;
            string customer_code = string.Empty;
            string receiptkey = string.Empty;
            string pickdetailkey = string.Empty;
            string status = string.Empty;
            string pre_qty = string.Empty;
            string qty_inv = string.Empty;
            string return_status = "";
            List<string> sqllists = new List<string>();
            List<string> Orsqllists = new List<string>();
            string strSql = string.Empty;
            string strSql1 = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("allot_confirm");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;

                userid = nodeSku.SelectSingleNode("userid").InnerText;
                whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                serial = nodeSku.SelectSingleNode("serial").InnerText;
                orderkey = nodeSku.SelectSingleNode("orderkey").InnerText;
                sku = nodeSku.SelectSingleNode("sku").InnerText;
                lpn= nodeSku.SelectSingleNode("lpn").InnerText;
                batch= nodeSku.SelectSingleNode("batch").InnerText;
                qty= nodeSku.SelectSingleNode("qty").InnerText;
                qty_inv= nodeSku.SelectSingleNode("qty_inv").InnerText;

                //通过出货单号
                strSql = @"select pickdetailkey,pickdetail.orderkey,pickdetail.line_no,fromloc,lot,lpn,pickdetail.lottable01,qty,pickdetail.customer_code,pickdetail.sku,pickdetail.skudescr,
                           PicStatus.description as status,orderdetail.pre_qty,orderdetail.picked_qty,orders.status as mainStatus,orderdetail.status as dtlStatus,pickdetail.uom 
                           from pickdetail
                           left join PicStatus on PicStatus.status=pickdetail.status
                           left join orderdetail on pickdetail.whseid=orderdetail.whseid and pickdetail.orderkey=orderdetail.orderkey and pickdetail.line_no=orderdetail.line_no
                           left join orders on orders.orderkey=pickdetail.orderkey and orders.whseid=pickdetail.whseid 
                           where   pickdetail.whseid='" + whseid + "' and pickdetail.orderkey='" + orderkey + "' and pickdetail.lpn='"+lpn+"' and pickdetail.lottable01='"+batch+ "' and pickdetail.sku='"+sku+"' and orders.product='1' and pickdetail.status='01'";
                DataTable ldt = App.GetDataTable(App.DBconWMS, strSql, "inv");
                #endregion

                sqllists.Clear();

                if (ldt.Rows.Count > 0)
                {
                    #region 参数源

                    line_no = ldt.Rows[0]["line_no"].ToString();
                    customer_code = ldt.Rows[0]["customer_code"].ToString();
                    sku = ldt.Rows[0]["sku"].ToString();
                    skudescr = ldt.Rows[0]["skudescr"].ToString();
                    uom = ldt.Rows[0]["uom"].ToString();
                    pickdetailkey = ldt.Rows[0]["pickdetailkey"].ToString();
                    //qty = ldt.Rows[0]["qty"].ToString();
                    status = ldt.Rows[0]["status"].ToString();
                    loc = ldt.Rows[0]["fromloc"].ToString();
                    lot = ldt.Rows[0]["lot"].ToString();
                    lpn = ldt.Rows[0]["lpn"].ToString();
                    //pre_qty = ldt.Rows[0]["pre_qty"].ToString();

                    #endregion

                    #region 数据更新
                    //拣货明细更新
                    strSql = "update pickdetail set status='03' where whseid='" + whseid + "' and pickdetailkey='" + pickdetailkey + "'";
                    sqllists.Add(strSql);

                    //更新出货表头及明细
                    strSql = "update orderdetail set picked_qty=picked_qty+" + qty + ",allocated_qty=allocated_qty-" + qty + ", status='" + status + "'";
                    strSql += " where whseid='" + whseid + "' and orderkey='" + orderkey + "' and line_no='" + line_no + "'";
                    sqllists.Add(strSql);

                    //明细历史记录
                    strSql = "insert into OrdHistoryDtl(whseid,orderkey,line_no,status,operation,sku,addwho,adddate) values ";
                    strSql += "('" + whseid + "','" + orderkey + "','" + line_no + "','" + status + "','拣货确认','"+ sku +"','" + userid + "','" + DateTime.Now + "')";
                    sqllists.Add(strSql);


                    //更新库存(增加拣货数量,减少分配数量)
                    strSql = "update inventory set ";
                    strSql += " allocated_qty=allocated_qty-"+qty+ ", ";
                    strSql += " picked_qty=picked_qty+"+qty+" ";
                    strSql += " where whseid='" + whseid + "' and customer_code='" + customer_code + "' and sku='" + sku + "' and lot='" + lot + "' and loc='" + loc + "' and lpn='" + lpn + "'";
                    sqllists.Add(strSql);

                    //更新表头
                    string ordStatus = "06";
                    strSql = "select orderkey from OrderDetail where orderkey='" + orderkey + "' and whseid='" + whseid + "' and  status<>'05'";
                    DataTable ldt_pick = App.GetDataTable(App.DBconWMS, strSql, "Orders");
                    if (ldt_pick.Rows.Count == 0)
                    {
                        ordStatus = "07";
                    }
                    strSql = "update orders set status='" + ordStatus + "' where whseid='" + whseid + "' and orderkey='" + orderkey + "'";
                    Orsqllists.Clear();
                    Orsqllists.Add(strSql);
                    App.ExecuteSQLScriptLists(Orsqllists, App.DBconWMS); // 单独更新订单主表

                    strSql = "insert into ordhistory(whseid,orderkey,status,operation,addwho,adddate) values ";
                    strSql += "('" + whseid + "','" + orderkey + "','" + ordStatus + "','拣货确认','" + userid + "','" + DateTime.Now + "')";
                    sqllists.Add(strSql);

                    //提交数据
                    if(!App.ExecuteSQLScriptLists(sqllists, App.DBconWMS))
                    {
                        return_status = "" + serial + "上架确认失败！！！ 原因：数据无法提交。";
                    }
                    #endregion

                    #region 构建XML报文
                    XmlNode orders_result = docreturn.CreateElement("allot_confirm");

                    XmlElement useridResult = docreturn.CreateElement("userid");
                    useridResult.InnerText = userid;
                    orders_result.AppendChild(useridResult);

                    XmlElement whseidResult = docreturn.CreateElement("whseid");
                    whseidResult.InnerText = whseid;
                    orders_result.AppendChild(whseidResult);

                    XmlElement serialResult = docreturn.CreateElement("serial");
                    serialResult.InnerText = serial;
                    orders_result.AppendChild(serialResult);

                    XmlElement orderResult = docreturn.CreateElement("orderkey");
                    orderResult.InnerText = orderkey;
                    orders_result.AppendChild(orderResult);

                    XmlElement skuResult = docreturn.CreateElement("sku");
                    skuResult.InnerText = sku;
                    orders_result.AppendChild(skuResult);

                    XmlElement qtyResult = docreturn.CreateElement("qty");
                    qtyResult.InnerText = qty;
                    orders_result.AppendChild(qtyResult);

                    XmlElement lpnResult = docreturn.CreateElement("lpn");
                    lpnResult.InnerText = lpn;
                    orders_result.AppendChild(lpnResult);

                    XmlElement locResult = docreturn.CreateElement("loc");
                    locResult.InnerText = loc;
                    orders_result.AppendChild(locResult);

                    XmlElement batchResult = docreturn.CreateElement("batch");
                    batchResult.InnerText = batch;
                    orders_result.AppendChild(batchResult);

                    XmlElement qty_allotResult = docreturn.CreateElement("qty_inv");
                    qty_allotResult.InnerText = qty_inv;
                    orders_result.AppendChild(qty_allotResult);

                    XmlElement return_statusResult = docreturn.CreateElement("return_status");
                    return_statusResult.InnerText = return_status;
                    orders_result.AppendChild(return_statusResult);

                    rootResult.AppendChild(orders_result);
                    #endregion

                }
                else
                {
                    #region 构建XML报文
                    return_status = "" + serial + "上架确认失败！！！ 原因：没有拣货信息";

                    XmlNode orders_result = docreturn.CreateElement("allot_confirm");

                    XmlElement useridResult = docreturn.CreateElement("userid");
                    useridResult.InnerText = userid;
                    orders_result.AppendChild(useridResult);

                    XmlElement whseidResult = docreturn.CreateElement("whseid");
                    whseidResult.InnerText = whseid;
                    orders_result.AppendChild(whseidResult);

                    XmlElement serialResult = docreturn.CreateElement("serial");
                    serialResult.InnerText = serial;
                    orders_result.AppendChild(serialResult);

                    XmlElement orderResult = docreturn.CreateElement("orderkey");
                    orderResult.InnerText = orderkey;
                    orders_result.AppendChild(orderResult);

                    XmlElement skuResult = docreturn.CreateElement("sku");
                    skuResult.InnerText = sku;
                    orders_result.AppendChild(skuResult);

                    XmlElement qtyResult = docreturn.CreateElement("qty");
                    qtyResult.InnerText = qty;
                    orders_result.AppendChild(qtyResult);

                    XmlElement lpnResult = docreturn.CreateElement("lpn");
                    lpnResult.InnerText = "";
                    orders_result.AppendChild(lpnResult);

                    XmlElement locResult = docreturn.CreateElement("loc");
                    locResult.InnerText = "";
                    orders_result.AppendChild(locResult);

                    XmlElement batchResult = docreturn.CreateElement("batch");
                    batchResult.InnerText = batch;
                    orders_result.AppendChild(batchResult);

                    XmlElement qty_allotResult = docreturn.CreateElement("qty_inv");
                    qty_allotResult.InnerText = "";
                    orders_result.AppendChild(qty_allotResult);

                    XmlElement return_statusResult = docreturn.CreateElement("return_status");
                    return_statusResult.InnerText = return_status;
                    orders_result.AppendChild(return_statusResult);


                    rootResult.AppendChild(orders_result);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlInventoryAllotProcess.cs-1", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlPrePickProcess:GetPDAPrePickData;" + "  " + ex.Message);
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }
    }
}
